package com.interauto.Controller.casepage;

import com.interauto.bean.web.CaseinfoWeb;
import com.interauto.bean.web.ResPonseinfo;
import com.interauto.service.web.CasePageService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * 操作用例相关的接口
 */
@Controller
@RequestMapping(value = "/api/case")
public class CaseApiController {
    private static final Logger logger= LoggerFactory.getLogger(CaseApiController.class);
    @Autowired
    private CasePageService casePageService;

    @RequestMapping(value = "/addcaseinfo",method = RequestMethod.POST)
    @ResponseBody
    public Object addcaseinfo(@RequestParam(value = "casename") String casename,
                              @RequestParam(value = "caseinfo") String caseinfo,
                              @RequestParam(value = "projectid") Integer projectid){
        if(StringUtils.isEmpty(casename) || projectid ==null || projectid<1){
            return new ResPonseinfo(-1,"请求参数为空");
        }
        CaseinfoWeb caseinfoWeb=new CaseinfoWeb();
        caseinfoWeb.setCaseinfo(caseinfo);
        caseinfoWeb.setCasename(casename);
        caseinfoWeb.setProjectid(projectid);
        int caseid=casePageService.addcaseinfo(caseinfoWeb);
        if(caseid<1){
            return new ResPonseinfo(caseid,"请求异常");
        }
        Map<String,String> casemap=new HashMap<>();
        casemap.put("caseid",String.valueOf(caseid));
        ResPonseinfo res=new ResPonseinfo(0,"ok");
        res.setData(casemap);
        return res;
    }

    @RequestMapping(value = "/upcaseinfo",method = RequestMethod.POST)
    @ResponseBody
    public Object upcaseinfo(@RequestParam(value = "casename") String casename,
                             @RequestParam(value = "caseinfo") String caseinfo,
                             @RequestParam(value = "caseid") Integer caseid){
        if(StringUtils.isEmpty(casename) || StringUtils.isEmpty(caseinfo) || caseid ==null || caseid<1){
            return new ResPonseinfo(-1,"请求参数为空");
        }
        CaseinfoWeb caseinfoWeb=new CaseinfoWeb();
        caseinfoWeb.setCaseinfo(caseinfo);
        caseinfoWeb.setCasename(casename);
        caseinfoWeb.setCaseid(caseid);
        casePageService.upcaseinfo(caseinfoWeb);
        return new ResPonseinfo(0,"ok");
    }


    @RequestMapping(value = "/getcaseinfo",method = RequestMethod.GET)
    @ResponseBody
    public Object getcaseinfo(@RequestParam(value = "caseid") Integer caseid){
        CaseinfoWeb caseinfoWeb=casePageService.getCaseinfo(caseid);
        if(caseinfoWeb == null){
            return new ResPonseinfo(-1,"未找到该用例信息");
        }
        return new ResPonseinfo(0,"ok",caseinfoWeb);
    }

    @RequestMapping(value = "/del",method = RequestMethod.GET)
    @ResponseBody
    public Object delcaseinfo(@RequestParam(value = "caseid") Integer caseid){
        casePageService.delcaseinfo(caseid);
        return new ResPonseinfo(0,"删除成功");
    }
}
